package cn.czh.system.controller;

import cn.czh.basic.util.AjaxResult;
import cn.czh.basic.util.PageList;
import cn.czh.system.domain.SystemDictionaryDetail;
import cn.czh.system.query.SystemDictionaryDetailQuery;
import cn.czh.system.service.ISystemDictionaryDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 字典明细接口
 */
@RestController
@RequestMapping("/systemdictionarydetail")
@Api(value = "字典明细接口类",description = "字典明细的CRUD操作")
public class SystemDictionaryDetailController {
    @Autowired
    private ISystemDictionaryDetailService systemDictionaryDetailService;

    /**
     * 接口:查询所有
     * @return
     */
    //http://localhost:8080/systemDictionaryDetail - Get
    @GetMapping
    @ApiOperation(value = "查询所有接口" )
    public List<SystemDictionaryDetail> findAll(){
        return systemDictionaryDetailService.findAll();
    }

    /**
     * 接口:查询单个
     * @param id
     * @return
     */
    //http://localhost:8080/systemDictionaryDetail/2 - Get
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个接口")
    public SystemDictionaryDetail findOne(@PathVariable("id") Long id){
        return systemDictionaryDetailService.findOne(id);
    }

    /**
     * 接口：分页查询
     * @param systemDictionaryDetailQuery
     * @return
     */
    //http://localhost:8080/systemDictionaryType - Post 参数通过json对象传递
    @PostMapping
    @ApiOperation(value = "分页查询接口")
    //@RequestBody - 从请求体中获取数据并赋值给systemDictionaryDetailQuery
    public PageList<SystemDictionaryDetail> queryPage(@RequestBody SystemDictionaryDetailQuery systemDictionaryDetailQuery){
        return systemDictionaryDetailService.queryPage(systemDictionaryDetailQuery);
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            systemDictionaryDetailService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            //return new AjaxResult(false,"删除成功");
            //链式语法
            return AjaxResult.me().setSuccess(false).setMsg("删除失败").setResultObj("请重新操作");
        }
    }

    /**
     * 接口：批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除接口")
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            systemDictionaryDetailService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            //return new AjaxResult(false,"删除成功");
            //链式语法
            return AjaxResult.me().setSuccess(false).setMsg("删除失败").setResultObj("请重新操作");
        }
    }

    @PutMapping
    @ApiOperation(value = "添加或者修改接口")
    //@RequestBody - 从请求体中获取数据并赋值给shop
    public AjaxResult addOrUpdate(@RequestBody SystemDictionaryDetail systemDictionaryDetail){
        try {
            //如果没有获取id就是添加
            if (systemDictionaryDetail.getId() == null){
                systemDictionaryDetailService.add(systemDictionaryDetail);
            }else {
                //否则就是修改
                systemDictionaryDetailService.update(systemDictionaryDetail);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            //return new AjaxResult(false,"操作失败");
            //链式语法
            return AjaxResult.me().setSuccess(false).setMsg("操作失败").setResultObj("请重新操作");
        }
    }

    /**
     * 抽屉
     * @param id
     * @return
     */
    //根据id查询所有的数据明细types
    @ApiOperation(value = "查询所有的明细types")
    @GetMapping("/types/{id}")
    public List<SystemDictionaryDetail> findAllType(@PathVariable("id") Long id){
        return systemDictionaryDetailService.loadAllType(id);
    }
}
